/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package introHilos;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pedroj
 */
public class PrintTask implements Runnable {
    
    private final int sleepTime; // random sleep time for thread
    private final String taskName; // name of task
    private final static Random generator = new Random();

    // constructor
    public PrintTask( String name ) {
        taskName = name; // set task name

        // pick random sleep time between 0 and 5 seconds
        sleepTime = generator.nextInt( 5000 ); // milliseconds
    } // end PrintTask constructor
    
    // method run contains the code that a thread will execute
    @Override
    public void run() {
        try {
            System.out.printf( "%s going to sleep for %d milliseconds.\n",taskName, sleepTime );
            Thread.sleep( sleepTime ); // put thread to sleep
            
            // print task name
            System.out.printf( "%s done sleeping\n", taskName );
        } // end method run
        catch (InterruptedException ex) {
            Logger.getLogger(PrintTask.class.getName()).log(Level.SEVERE, null, ex);
        }
    } // end method run
} // end class PrintTask}
